package com.fly;

import java.io.IOException;
import java.net.InetAddress;

import javax.servlet.ServletContext;

import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class TotpApplication implements CommandLineRunner
{
    @Value("${server.port}")
    Integer port;
    
    @Autowired
    ServletContext servletContext;
    
    public static void main(String[] args)
        throws Exception
    {
        SpringApplication.run(TotpApplication.class, args);
    }
    
    @Override
    public void run(String... args)
    {
        try
        {
            if (SystemUtils.IS_OS_WINDOWS && port > 0)
            {
                String ip = InetAddress.getLocalHost().getHostAddress();
                String url = "http://" + ip + ":" + port + servletContext.getContextPath();
                Runtime.getRuntime().exec("cmd /c start /min " + url + "/00fly/");
            }
        }
        catch (IOException e)
        {
            log.error(e.getMessage(), e);
        }
    }
}
